<template>
	<view class="container">
		<view class="card">

			<text class="title">登录</text>
			<view class="form-group">
				<text class="label">请输入用户名</text>
				<input type="text" v-model="name" placeholder="用户名" class="input" />
			</view>
			<view class="form-group">
				<text class="label">请输入密码</text>
				<input type="password" v-model="password" placeholder="密码" class="input" />
			</view>
			<button class="btn" @click="handleLogin()">登录</button>
			<navigator url="/pages/register/register" class="link">没有账号？去注册</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				password: ''

			};
		},
		methods: {
			handleLogin() {
				console.log("handleLogin()-->登录验证");
				if (!this.name) {
					console.log("handleLogin()-->登录验证");
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					});
					return;
				}
				if (!this.password) {
					console.log("handleLogin()-->登录验证");
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}
				uniCloud.callFunction({
						name: 'checkUser',
						data: {
							name: this.name,
							password: this.password
						},
						success:(msg) => {
							console.log("checkUser" + JSON.stringify(msg));
							const{code,data}=msg.result;
							if(code===200){
								uni.showToast({
									title: '登录成功！',
									icon:'success'
								});
								uni.reLaunch({
									url:"/pages/NewCardPage/NewCardPage"
								});
								getApp().setUserInfo(this.name,msg.result.data);
								uni.setStorage({
									key: 'Account',
									data: this.name,
									success:()=>{
										console.log("Account存储成功");
									}
									});
							
							
							}
							else if(code===201){
								uni.showToast({
									title: '密码错误',
									icon:'none'
								});
							}else if(code===300){
								uni.showToast({
									title: '用户不存在',
									icon:'none'
								});
							}else if(code===400){
								uni.showToast({
									title: '输入信息无效',
									icon:'none'
								});
							}else if(code===500||code===600){
								uni.showToast({
									title: '服务器异常',
									icon:'none'
								});
							}
							
						},
						
							fail(e) {
								console.log("Account存储失败:" + e);
							}
							
							
						});
						
					}
				}
			};
</script>

<style>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #f0f2f5;
		padding: 20px;
	}

	.card {
		background-color: #fff;
		padding: 20px;
		border-radius: 8px;
		box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
		width: 100%;
		max-width: 350px;
	}

	.title {
		display: block;
		text-align: center;
		font-size: 24px;
		font-weight: bold;
		color: #333;
		margin-bottom: 25px;
	}

	.form-group {
		margin-bottom: 20px;
	}

	.label {
		display: block;
		margin-bottom: 8px;
		color: #666;
		font-size: 14px;
	}

	.input {
		width: 90%;
		padding: 12px;
		border: 1px solid #ddd;
		border-radius: 4px;
		font-size: 16px;
	}

	.btn {
		width: 95%;
		padding: 12px;
		background-color: #1677ff;
		color: #fff;
		border-radius: 4px;
		font-size: 16px;
		margin-top: 10px;
	}

	.link {
		display: block;
		text-align: center;
		margin-top: 15px;
		font-size: 14px;
		color: #1677ff;
		text-decoration: none;
	}
</style>